package com.jiuyin.dianjing.ui.activity.login;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hyb.library.PreventKeyboardBlockUtil;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.ui.LoginFragment;

import butterknife.BindView;

import static com.jiuyin.dianjing.constant.AppConstant.URL_FW;
import static com.jiuyin.dianjing.constant.AppConstant.URL_YS;
import static com.jiuyin.dianjing.ui.fragment.ui.LoginFragment.KEY_BUSINESS_TYPE;
import static com.jiuyin.dianjing.ui.fragment.ui.LoginFragment.TYPE_REGISTER;

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.tv_protocol)
    TextView tvProtocol;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.iv_back)
    View ivBack;
    private int mType;
    private LoginFragment fragment;

    @Override
    protected void beforeSetContentView() {
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        mType = getIntent().getIntExtra(KEY_BUSINESS_TYPE, TYPE_REGISTER);
    }

    @Override
    protected void initView() {
        if (mType == TYPE_REGISTER) {
            btSubmit.setText("注册");
            tvProtocol.setVisibility(View.VISIBLE);
            SpannableStringBuilder builder = new SpannableStringBuilder(getString(R.string.protocol));
            URLSpan ab = new URLSpan(URL_FW);
            builder.setSpan(ab, 8, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ab = new URLSpan(URL_YS);
            builder.setSpan(ab, 17, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvProtocol.setMovementMethod(new LinkMovementMethod());
            tvProtocol.setText(builder);
        } else {
            btSubmit.setText("完成");
            tvProtocol.setVisibility(View.INVISIBLE);
        }
        fragment = LoginFragment.newInstance(mType);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.layout_fragment, fragment)
                .commit();
        btSubmit.setOnClickListener(v -> {
            fragment.submit();
        });
        ivBack.setOnClickListener(v -> {
            onBackPressed();
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        PreventKeyboardBlockUtil.getInstance(this).setBtnView(btSubmit).register();
    }

    @Override
    protected void onPause() {
        super.onPause();
        PreventKeyboardBlockUtil.getInstance(this).unRegister();
    }
}
